package com.xlkj.ms.tools.core.commons;

/**
 * 默认的返回码。这几个返回码不应去改变，使用方可以在这个基础上进行扩展
 * @author zhuqifeng
 * @date 2021/11/10 11:54
 * @package com.xlkj.ms.tools.core.commons
 */
public enum DefaultRetCode {

    /** 成功 */
    SUCCESS("200", "成功"),

    /** 接口通信异常 **/
    INTERFACE_ERROR("997", "接口通信异常"),

    /** 参数错误 */
    ILLEGAL_ARGUMENT("998", "参数错误：%wd"),

    /** 系统异常 */
    SYS_ERROR("999", "系统异常"), ;

    private final String code;
    private String msg;

    DefaultRetCode(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 变更错误信息（替换某些动态的通配符：%wd）
     *
     * @param retCode
     * @param msgs
     * @return
     */
    public static String changeRetCode(DefaultRetCode retCode, String... msgs) {
        if (null == retCode) {
            return null;
        }
        if (retCode.getMsg().contains("%wd")) {
            StringBuilder sb = new StringBuilder();
            String retMsg = "start_" + retCode.getMsg() + "_end";
            String[] arr = retMsg.split("%wd");
            for (int i = 0; i < arr.length; i++) {
                sb.append(arr[i]);
                if (i == arr.length - 1) {
                    break;
                }
                if (null == msgs || 0 == msgs.length || i >= msgs.length) {
                    sb.append(" ");
                } else {
                    for (int j = i; j < msgs.length;) {
                        sb.append(msgs[j]);
                        break;
                    }
                }
            }
            String msg = sb.toString();
            msg = msg.substring(6, msg.indexOf("_end"));
            return msg;
        }
        return retCode.getMsg();
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }
}
